import LevelBaseControl from "../../../LevelCommon/LevelBaseControl";

export default class MoveLock extends Laya.Script3D {
    /** 当前所有位置信息*/
    public curAllPoses: Array<Laya.Transform3D> = new Array<Laya.Transform3D>();
    /** 当前选择的物体*/
    //MoveLockKey
    public curSelectKey: any;

    /** 当前所有的移动物体*/
    //MoveLockKey []curAllMoveLockKeys;
    /** 正确索引*/
    public correctIndex: Array<number> =  new Array<number>();;
    /** 当前是否正确了*/
    public isCorrect: boolean = false;

    private isPlayingTween: boolean;

    public ChildMoveLockKeysCurIndex:Array<number> = new Array<number>();

    onStart() {
        for (let index = 0; index < this.correctIndex.length; index++) {
            this.ChildMoveLockKeysCurIndex.push(0);
        }
    }
    /** 设置当前选中了某个物体*/
    public SelectKey(moveLockKey: any) {
        this.curSelectKey = moveLockKey;
    }
    /** 当前选中的和另外一个物体交换*/
    public ChangeKey(moveLockKey: any) {
        if (this.curSelectKey != null && this.curSelectKey != moveLockKey) {
            /** 当前选中物体的位置索引*/
            let curSelectIndex: number = this.curSelectKey.curPosIndex;
            /** 要交换的索引*/
            let changeIdnex = moveLockKey.curPosIndex;

            /** 交换*/
            this.isPlayingTween = true;
            this.curSelectKey.DoMovePos(this.curAllPoses[changeIdnex], changeIdnex);
            moveLockKey.DoMovePos(this.curAllPoses[curSelectIndex], curSelectIndex,
                /** 动画结束判断是否正确*/
                Laya.Handler.create(this, () => {
                    this.isPlayingTween = false;
                    this.curSelectKey.SetSelect(false);
                    this.curSelectKey = null;
                    let length = this.correctIndex.length;
                    for (let i = 0; i < length; i++) {
                        /** 有不一样的就返回*/
                        if (this.correctIndex[i] != this.ChildMoveLockKeysCurIndex[i]) {
                            return;
                        }
                    }

                    /** 密码正确*/
                    console.log("密码正确！！！！！！！！！！");
                    this.isCorrect = true;
                    let curLevelControl: LevelBaseControl = window.gameFacade.LevelMng.GetCurLevelControl();
                    curLevelControl.DoInteractObjLogic(this.owner.name + "_MoveLock_Unlock");
                }))
        }
    }
    /** 是否正在播动画*/
    public IsPlayingTween(): boolean {
        return this.isPlayingTween;
    }
}